<?php

//	Author: 	Christos Polydorou
//	Email:		std04237@di.uoa.gr
//	Purpose: 	Create the values of the attributes of an entity

require_once ("../DB/DB.php");
require_once ("../Frameworks/Objects/user.php");
require_once ("../Frameworks/Objects/category.php");
require_once ("../Frameworks/Objects/entity.php");
require_once ("../Frameworks/Objects/attribute.php");

function DB_entity_updateattributevalues($entity)
{
    $attributes = $entity->GetAttributes();
    foreach ($attributes as $attr)
        DB_entity_updateattributevalue($entity, $attr);
    return DB_OK;
}

function DB_entity_updateattributevalue($entity, $attribute)
{

    global $conn;

    // The name of the stored procedure
    $sp_name = "usp_entity_updateattributevalue";

    // The return variable
    $retval;

    // Get the values
    $entityid = $entity->GetId();
    $attributename = $attribute->GetName();
    $attributes = $entity->GetAttributes();
    $i = '0';
    $value = '0';
    foreach ($attributes as $attr)
    {
         /* get the value for the attribute */
        if ($attr->GetName() == $attributename)
        {
            $value = $attr->GetValue();
            break;
        }
    }
    // Create a new statement
    $stmt = mssql_init($sp_name, $conn);

    // Bind values
    mssql_bind($stmt, "RETVAL", $retval, SQLINT4, true, false, 1);
    mssql_bind($stmt, "@entityid", $entityid, SQLINT4, false, false, 1);
    mssql_bind($stmt, '@attributename', $attributename, SQLVARCHAR, false, false, 50);
    mssql_bind($stmt, '@value', $value, SQLVARCHAR, false, false, 50);

    // Execute the statement
    $status = mssql_execute($stmt);
    if (!$status)
        return DB_ERROR_CANNOTGETVALUESFORENTITY;
    else
        return DB_OK;
}

?>